﻿using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Text;

namespace YLBaseCommon
{
    public class RemoteServices<T>
    {
        IMemoryCache mc = new MemoryCache(Options.Create(new MemoryCacheOptions()));

        private string TokenName = "authToken";


        private static string SiteRootPath = "https://apicms.yla520.com";

        private string Bearer { get {
                string value = "";
                mc.TryGetValue(TokenName, out value);
                if (string.IsNullOrEmpty(value))
                {
                    string url = string.Format("https://apidevelop.yla520.com/{1}/{2}", ApiSite, "Oauth", "Token");
                    string json = HttpService.HttpPostData(url, "{\"UserName\":\"admin\",\"UserPwd\":\"123456\"}", "");
                    dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
                    string accessToken = obj.access_token;
                    DateTime expTime = DateTime.Now.AddDays(6);
                    if (mc.TryGetValue(TokenName,out value))
                    {
                        mc.Set<string>(TokenName, accessToken, expTime);
                    }
                    else
                    {
                        using (ICacheEntry entry = mc.CreateEntry(TokenName))
                        {
                            entry.Value = accessToken;
                            entry.AbsoluteExpiration = expTime;
                        }
                    }
                    return accessToken;
                }
                else
                {
                    return value;
                }             
            } }


        string ApiSite = SiteRootPath+"/api";

        public RemoteRs<T> CallJson(string area,string function, Dictionary<string, object> data, string type="post")
        {
            try
            {
                string url = "";
                //参数的问题
                switch (type.ToLower())
                {
                    case "get":
                        url =string.Format("{0}/{1}/{2}?client=pc{3}", ApiSite, area, function, DecodeGetData(data));
                        string getRs = HttpService.HttpGetData(url, Bearer);
                        return Newtonsoft.Json.JsonConvert.DeserializeObject<RemoteRs<T>>(getRs);
                    case "post":
                        url = string.Format("{0}/{1}/{2}?client=pc", ApiSite, area, function);
                        string dataJson =Newtonsoft.Json.JsonConvert.SerializeObject(data);
                        string postRs = HttpService.HttpPostData(url, dataJson, Bearer);
                        return Newtonsoft.Json.JsonConvert.DeserializeObject<RemoteRs<T>>(postRs);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return null;
        }

        private string DecodeGetData(Dictionary<string, object> data)
        {
            StringBuilder sb = new StringBuilder();
            if (data.Count >0)
            {
                sb.Append("&");
            }
            foreach (var item in data)
            {
                
                sb.Append(item.Key+"="+item.Value+"&");
            }
            return sb.ToString().TrimEnd('&');
        }
        


    }
}
